package com.gopay.checkorder.dao.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.gopay.checkorder.dao.OrderAdRecordDAO;
import com.gopay.common.base.dao.impl.BaseDAOImpl;
import com.gopay.common.domain.cps.CpsOrderAdjustRecord;
import com.gopay.common.util.DateUtils;

/**
 * 订单调整DAO实现
 * 
 * @OrderAdRecordDAOImpl.java
 * @author yaobo
 * @2013-11-8 上午09:38:05 www.gopay.com.cn Inc.All rights reserved.
 */
@Repository(value = "orderAdRecordDAO")
public class OrderAdRecordDAOImpl extends BaseDAOImpl<CpsOrderAdjustRecord, Long> implements OrderAdRecordDAO {

    @Override
    public List<CpsOrderAdjustRecord> getUnCheckList(String adjustResult, int adjustCounts, int pageIndex, int pageSize) {
        StringBuffer hql = new StringBuffer("from CpsOrderAdjustRecord ");
        hql.append("where adjustResult = :adjustResult ");
        hql.append("and adjustCounts = :adjustCounts");

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("adjustResult", adjustResult);
        params.put("adjustCounts", adjustCounts);

        return this.findByHql(hql.toString(), params, pageIndex, pageSize);
    }

    @Override
    public int getUnCheckCount(String adjustResult, int adjustCounts) {
        StringBuffer hql = new StringBuffer("select count(*) from CpsOrderAdjustRecord ");
        hql.append("where adjustResult = :adjustResult ");
        hql.append("and adjustCounts = :adjustCounts");

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("adjustResult", adjustResult);
        params.put("adjustCounts", adjustCounts);

        return this.findCountByHql(hql.toString(), params);
    }

    @Override
    public int lock(String gopayOutOrderId) {
        Date currDate = DateUtils.getSystemDate();

        StringBuffer hql = new StringBuffer("update CpsOrderAdjustRecord ");
        hql.append("set adjustResult = :adjustResult,");
        hql.append("lastUpdTime = :lastUpdTime,");
        hql.append("adjustTime = :adjustTime ");
        hql.append("where gopayOutOrderId = :gopayOutOrderId");

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("adjustResult", "05");
        params.put("lastUpdTime", currDate);
        params.put("adjustTime", currDate);
        params.put("gopayOutOrderId", gopayOutOrderId);

        return this.updateByHql(hql.toString(), params);
    }

    @Override
    public CpsOrderAdjustRecord getByGopayOutOrderId(String gopayOutOrderId) {
        String hql = "from CpsOrderAdjustRecord where gopayOutOrderId = :gopayOutOrderId";

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("gopayOutOrderId", gopayOutOrderId);

        List<CpsOrderAdjustRecord> list = this.findByHql(hql, params);
        if (list != null && list.size() > 0) {
            return list.get(0);
        }

        return null;
    }

}
